package cn.ffcs.uec.staff.action;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.manager.CertIdVarBaseInfoManager;
import cn.ffcs.uec.common.model.CertIdVarBaseInfo;
import cn.ffcs.uec.staff.action.bean.UecStaffInfoAddConfirmBean;
import cn.ffcs.uec.staff.constant.UecStaffConstants;
import cn.ffcs.uec.staff.manager.UecStaffInfoManager;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uom.common.util.ApplicationContextUtil;

@Controller
@Scope("prototype")
public class UecStaffInfoAddConfirmComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecStaffInfoAddConfirmBean bean = new UecStaffInfoAddConfirmBean();

	private UecStaffInfo uecStaffInfo;
	
	private UecStaffInfo qryUecStaffInfo;

	private UecStaffInfoManager uecStaffInfoManager = (UecStaffInfoManager) ApplicationContextUtil
			.getBean("uecStaffInfoManager");

	private CertIdVarBaseInfoManager certIdVarBaseInfoManager = (CertIdVarBaseInfoManager) ApplicationContextUtil
			.getBean("certIdVarBaseInfoManager");
	
	/**
	 * 操作类型
	 */
	private String opType;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 */
	public void onCreate$uecStaffInfoAddConfirmWindow() {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {

	}

	/**
	 * bindBean
	 */
	public void bindBean() {
		opType = (String) arg.get("opType");
		if ("addConfirm".equals(opType)) {
			this.bean.getUecStaffInfoAddConfirmWindow().setTitle("新增校验");
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		if ("addConfirm".equals(opType)) {
			// 获取人员姓名
			String idName = this.bean.getStaffName().getValue();
			// 获取证件号码
			String idNumber = this.bean.getIdNumber().getValue();
			// 获取人员类别
			uecStaffInfo = uecStaffInfoManager.findUecStaffInfoOnly(idName,idNumber);
			if (null == uecStaffInfo) {
				CertIdVarBaseInfo certIdVarBaseInfo = certIdVarBaseInfoManager
						.getCertIdVarInfo(idName, idNumber);
				if (null != certIdVarBaseInfo) {
					// 调用实名制校验接口开始
					
					// 调用结束
					uecStaffInfo = new UecStaffInfo();
					uecStaffInfo.setName(idName);
					uecStaffInfo.setIdNumber(idNumber);
	
					this.onCancel();
					this.addStaffInfo();
					
				} else {
					ZkUtil.showError("证件信息校验失败！", null);
				}
			}else{
				ZkUtil.showError("该账号已存在！", null);
			}

		}
	}
	/**
	 * 多弹框，事件获取，得到对象并下沉,重要！！
	 */
	public void getUecStaffInfo(){
		qryUecStaffInfo = uecStaffInfo;
		Events.sendEvent("onOK", this.self, qryUecStaffInfo);
	}

	/**
	 * 新增人员基本信息
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void addStaffInfo() throws Exception, InterruptedException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("opType", "add");
		map.put("uecStaffInfo", uecStaffInfo);
		Window window = (Window) Executions.createComponents(
				UecStaffConstants.UEC_STAFF_INFO_EDIT_ZUL, null, map);
		window.doModal();
		window.addEventListener("onOK", new EventListener() {
			@Override
			public void onEvent(Event event) throws Exception {
				if (event.getData() != null) {
					uecStaffInfo = (UecStaffInfo) event.getData();
					getUecStaffInfo();
				}
			}
		});
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecStaffInfoAddConfirmWindow().onClose();
	}

}
